package service

import (
	"duoke/model"
	"duoke/tables"
	"duoke/utils/gjson"
	"duoke/utils/orm"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
)

var TongueDiagnosis = new(tongueDiagnosisService)

type tongueDiagnosisService struct{}

func (s *tongueDiagnosisService) Save(c *gin.Context, req *model.RspResult) error {
	user, err := Request.User(c)
	if err != nil {
		return err
	}
	marshal, err := json.Marshal(req.Data.InquiryQuestions)
	if err != nil {
		return err
	}
	m := tables.TongueDiagnosis{
		LogId:            req.LogId,
		SessionId:        req.Data.SessionId,
		InquiryQuestions: string(marshal),
		UserId:           user.Id,
		Acid:             user.Acid,
	}
	insert, err := orm.Db.Insert(&m)
	if err != nil {
		return err
	}
	if insert == 0 {
		return fmt.Errorf("保存失败")
	}
	return nil
}
func (s *tongueDiagnosisService) Report(c *gin.Context, req *model.RspReport) error {
	user, err := Request.User(c)
	if err != nil {
		return err
	}
	data := req.Data
	m := tables.TdReport{
		Score:                data.Score,
		Features:             gjson.StructOrMap2Json(data.Features),
		PhysiqueName:         data.PhysiqueName,
		PhysiqueAnalysis:     data.PhysiqueAnalysis,
		TypicalSymptom:       data.TypicalSymptom,
		RiskWarning:          data.RiskWarning,
		SyndromeName:         data.SyndromeName,
		SyndromeIntroduction: data.SyndromeIntroduction,
		Advices:              gjson.StructOrMap2Json(data.Advices),
		Answers:              gjson.StructOrMap2Json(data.Answers),
		TfDetectMatches:      gjson.StructOrMap2Json(data.TfDetectMatches),
		Acid:                 user.Acid,
		UserId:               user.Id,
	}
	insert, err := orm.Db.Insert(&m)
	if err != nil {
		return err
	}
	if insert == 0 {
		return fmt.Errorf("保存失败")
	}
	return nil
}
